package com.chanyachting.emall.action.member;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.form.MemberForm;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.serviceImpl.MemberServiceImpl;
import com.chanyachting.emall.util.RequestUtil;

public class UpdatePassword extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		MemberForm mform=(MemberForm)form;
		
		RequestUtil ru=new RequestUtil(request);
		
		Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION,new Member());
		
		if(member.getPassword().equals(mform.getPassword())){
			
			if(!mform.getNewPassword().equals("") && mform.getNewPassword().equals(mform.getConfNewPassword())){
				
				MemberService memberService=new MemberServiceImpl();
				
				boolean temp=memberService.updatePassword(mform.getNewPassword(), member);
				
				if(temp){
					
					//更新session中的会员密码
					member.setPassword(mform.getNewPassword());
					
					ru.setMsg("登录密码更新成功！");
					
				}else{
					
					ru.setMsg("登录密码更新失败！请稍候重试。");
				}
				
			}else{
				
				ru.setMsg("新密码确认不一致！请确认新密码。");
			}
			
			
			
		}else{
			
			ru.setMsg("登录密码不正确！请确认登录密码。");
			
		}
		

		return mapping.findForward("updatepassword");
	}
	
	

}
